#### data ####
library(plspm)
library(ggplot2)
library(e1071)
data_sp <- read.csv("DataEthicsPerformance.csv")

#### pls set up ####
# inner Matrix
J   = c(0,0,0,0,0,0,0,0,0)
R   = c(0,0,0,0,0,0,0,0,0)
E   = c(0,0,0,0,0,0,0,0,0)
U   = c(0,0,0,0,0,0,0,0,0)
D   = c(0,0,0,0,0,0,0,0,0)
MM  = c(1,1,1,1,1,0,0,0,0)
CB  = c(0,0,0,0,0,1,0,0,0)
IRP = c(0,0,0,0,0,1,1,0,0)
AA  = c(0,0,0,0,0,0,0,1,0)
Path_sp = rbind(J, R, E, U, D, MM, CB, IRP, AA)
innerplot(Path_sp, box.size = 0.08, curve = 0.1, box.cex =1.6)

# outer Matrix
sp_blocks = list(1:2,3:4,5:6,7:9,10:11,12:15,16:18,19:20,21:22)
sp_modes = rep("A", 9)

#### running PLS-PM ####
Pls_sp = plspm(data_sp, Path_sp, sp_blocks, modes = sp_modes, scheme = "path", boot.val = TRUE, br = 5000)
plot(Pls_sp, box.size = 0.05, curve = 0.1, arr.pos = 0.39, cex.txt = 1.2, box.cex =1.2)

#### analalysis results ####
# outer model results
Pls_sp$unidim
# Pls_sp$outer_model
ggplot(data = Pls_sp$outer_model,aes(x = name, y = loading, fill = block)) +
        geom_bar(stat = 'identity', position = 'dodge') +
        geom_hline(yintercept = 0.7, color = 'gray50') +
        theme(axis.text.x = element_text(angle = 90,size =15),axis.text.y = element_text(size =15))
Pls_sp$crossloadings

# inner model results
#Pls_sp$inner_model
Pls_sp$inner_summary
Pls_sp$boot$paths